﻿using EMall.Domain;
using Microsoft.AspNetCore.Identity;
using System.Text.Json;

namespace EMall.IdentityService.Users
{
    public class User : IdentityUser<int>, IAggregateRoot<int>
    {
        public virtual bool Enabled { get; private set; }
        public virtual string Name { get; private set; }
        public virtual string Avatar { get; private set; }
        public User(string userName, bool enabled) : base(userName)
        {
            Enabled = enabled;
            AddDomainEvent(new EntityCreatedDomainEvent<User>(this));
        }
        public User(string userName, bool enabled, string name, string avatar, string email, string phoneNumber) : this(userName, enabled)
        {
            Name = name;
            Avatar = avatar;
            Email = email;
            PhoneNumber = phoneNumber;
            AddDomainEvent(new EntityCreatedDomainEvent<User>(this));
        }
        public void Update(bool enabled, string name, string avatar, string email, string phoneNumber)
        {
            var originJson = JsonSerializer.Serialize(this);
            Name = name;
            Avatar = avatar;
            Email = email;
            PhoneNumber = phoneNumber;
            Enabled = enabled;
            AddDomainEvent(new EntityUpdatedDomainEvent<User>(originJson, this));
        }
        public IReadOnlyList<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();
        protected List<IDomainEvent> _domainEvents = new List<IDomainEvent>();
        protected virtual void AddDomainEvent(IDomainEvent eventItem) => _domainEvents.Add(eventItem);
        public virtual void ClearDomainEvents() => _domainEvents.Clear();
    }
}
